package com.ruoyi.process.controller;

import java.util.List;

import com.ruoyi.main.service.IMainUnitesService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.process.domain.ProcessSellrecord;
import com.ruoyi.process.service.IProcessSellrecordService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 销售记录Controller
 * 
 * @author chao
 * @date 2024-07-26
 */
@Controller
@RequestMapping("/process/sellrecord")
public class ProcessSellrecordController extends BaseController
{
    private String prefix = "process/sellrecord";

    @Autowired
    private IProcessSellrecordService processSellrecordService;

    @Autowired
    private IMainUnitesService mainUnitesService;

    @RequiresPermissions("process:sellrecord:view")
    @GetMapping()
    public String sellrecord()
    {
        return prefix + "/sellrecord";
    }

    /**
     * 查询销售记录列表
     */
    @RequiresPermissions("process:sellrecord:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(ProcessSellrecord processSellrecord)
    {
        startPage();
        List<ProcessSellrecord> list = processSellrecordService.selectProcessSellrecordList(processSellrecord);
        return getDataTable(list);
    }

    /**
     * 导出销售记录列表
     */
    @RequiresPermissions("process:sellrecord:export")
    @Log(title = "销售记录", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(ProcessSellrecord processSellrecord)
    {
        List<ProcessSellrecord> list = processSellrecordService.selectProcessSellrecordList(processSellrecord);
        ExcelUtil<ProcessSellrecord> util = new ExcelUtil<ProcessSellrecord>(ProcessSellrecord.class);
        return util.exportExcel(list, "销售记录数据");
    }

    /**
     * 新增销售记录
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        model.addAttribute("uniteNames",mainUnitesService.getAllNormalNames());
        return prefix + "/add";
    }

    /**
     * 新增保存销售记录
     */
    @RequiresPermissions("process:sellrecord:add")
    @Log(title = "销售记录", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(ProcessSellrecord processSellrecord)
    {
        return toAjax(processSellrecordService.insertProcessSellrecord(processSellrecord));
    }

    /**
     * 修改销售记录
     */
    @RequiresPermissions("process:sellrecord:edit")
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") Long id, ModelMap mmap)
    {
        ProcessSellrecord processSellrecord = processSellrecordService.selectProcessSellrecordById(id);
        mmap.put("processSellrecord", processSellrecord);
        mmap.put("uniteNames",mainUnitesService.getAllNormalNames());
        return prefix + "/edit";
    }

    /**
     * 修改保存销售记录
     */
    @RequiresPermissions("process:sellrecord:edit")
    @Log(title = "销售记录", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(ProcessSellrecord processSellrecord,Model model)
    {
        model.addAttribute("uniteNames",mainUnitesService.getAllNormalNames());
        return toAjax(processSellrecordService.updateProcessSellrecord(processSellrecord));
    }

    /**
     * 删除销售记录
     */
    @RequiresPermissions("process:sellrecord:remove")
    @Log(title = "销售记录", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(processSellrecordService.deleteProcessSellrecordByIds(ids));
    }
}
